package com.atguigu.msmservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.msmservice.service.Msmservice;
import com.atguigu.msmservice.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

//@RestController
//@CrossOrigin
//@Api(tags = "腾讯云短信验证")
//@RequestMapping("/edumsm/msm")
//public class MsmController {
//    @Autowired
//    private Msmservice msmservice;
//    //发送短信的方法
//    @GetMapping("send/{phone}")
//    public R sendMsm(@PathVariable String phone){
//        //生成随机数，传递腾讯云进行发送
//        String code = RandomUtil.getFourBitRandom();
//        HashMap<String, Object> param = new HashMap<>();
//        param.put("code",code);
//        //调用方法验证是否发送成功
//        boolean isSend=msmservice.send(param,phone);
//       if (isSend){
//           return R.ok();
//       }else {
//           return R.error().message("发送短信失败！");
//       }
//    }
//}
@RestController
//@CrossOrigin
@Api(tags = "腾讯云短信验证")
@RequestMapping("/edumsm/msm")
public class MsmController {

    @Autowired
    private Msmservice msmservice;

    //获取手机号发送验证码
    @GetMapping("send/{phone}")
    @ApiOperation(value = "发送验证码")
    public R sendMsm(@ApiParam("用户手机号") @PathVariable String phone) {

        String verCode = msmservice.send(phone);
        return verCode != "" ? R.ok().data("", verCode) : R.error().message("短信发送失败");
    }
}